package com.zc.homePage.service.impl;

import com.ruoyi.common.core.domain.BaseEntity;
import com.zc.homePage.domain.ChargeCustomOrder;
import com.zc.homePage.mapper.HomePageMapper;
import com.zc.homePage.service.IHomePageService;
import com.zc.util.ArithmeticUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;

/**
 * @Author glb
 * @Date 2023/10/19 10:59
 * @Version 1.0
 */
@Service
public class HomePageServiceImpl implements IHomePageService {

    @Resource
    private HomePageMapper homePageMapper;

    private static final DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd");

    private static final SimpleDateFormat SIMPLE_DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd");

    private static final String[] weekDays = {"周日", "周一", "周二", "周三", "周四", "周五", "周六"};


    @Override
    public Map getLinkRelativeRatioCount() {
        //获取当前时间
        LocalDateTime todayLocalDateTime = LocalDateTime.now();
        LocalDateTime yesterdayLocalDateTime12 = todayLocalDateTime.minusDays(1);
        String today = todayLocalDateTime.format(DATE_TIME_FORMATTER);
        String yesterday = yesterdayLocalDateTime12.format(DATE_TIME_FORMATTER);
        Map linkRelativeRatioCount = homePageMapper.getLinkRelativeRatioCount(new BaseEntity(), today, yesterday);
        double todayPower = linkRelativeRatioCount.containsKey("todayPower") ? Double.parseDouble(String.valueOf(linkRelativeRatioCount.get("todayPower"))) : 0;
        double todayCount = linkRelativeRatioCount.containsKey("todayCount") ? Double.parseDouble(String.valueOf(linkRelativeRatioCount.get("todayCount"))) : 0;
        double todayMoney = linkRelativeRatioCount.containsKey("todayMoney") ? Double.parseDouble(String.valueOf(linkRelativeRatioCount.get("todayMoney"))) : 0;
        double todayRegister = linkRelativeRatioCount.containsKey("todayRegister") ? Double.parseDouble(String.valueOf(linkRelativeRatioCount.get("todayRegister"))) : 0;
        double yesterdayPower = linkRelativeRatioCount.containsKey("yesterdayPower") ? Double.parseDouble(String.valueOf(linkRelativeRatioCount.get("yesterdayPower"))) : 0;
        double yesterdayCount = linkRelativeRatioCount.containsKey("yesterdayCount") ? Double.parseDouble(String.valueOf(linkRelativeRatioCount.get("yesterdayCount"))) : 0;
        double yesterdayMoney = linkRelativeRatioCount.containsKey("yesterdayMoney") ? Double.parseDouble(String.valueOf(linkRelativeRatioCount.get("yesterdayMoney"))) : 0;
        double yesterdayRegister = linkRelativeRatioCount.containsKey("yesterdayRegister") ? Double.parseDouble(String.valueOf(linkRelativeRatioCount.get("yesterdayRegister"))) : 0;
        double todayPowerQOQ = yesterdayPower != 0 ? ArithmeticUtils.mul(ArithmeticUtils.div(ArithmeticUtils.sub(todayPower, yesterdayPower), yesterdayPower), 100, 2) : 0;
        double todayCountQOQ = yesterdayCount != 0 ? ArithmeticUtils.mul(ArithmeticUtils.div(ArithmeticUtils.sub(todayCount, yesterdayCount), yesterdayCount), 100, 2) : 0;
        double todayMoneyQOQ = yesterdayMoney != 0 ? ArithmeticUtils.mul(ArithmeticUtils.div(ArithmeticUtils.sub(todayMoney, yesterdayMoney), yesterdayMoney), 100, 2) : 0;
        double todayRegisterQOQ = yesterdayRegister != 0 ? ArithmeticUtils.mul(ArithmeticUtils.div(ArithmeticUtils.sub(todayRegister, yesterdayRegister), yesterdayRegister), 100, 2) : 0;
        HashMap hashMap = new HashMap();
        hashMap.put("todayPower", todayPower);
        hashMap.put("todayPowerQOQ", todayPowerQOQ);
        hashMap.put("todayCount", todayCount);
        hashMap.put("todayCountQOQ", todayCountQOQ);
        hashMap.put("todayMoney", todayMoney);
        hashMap.put("todayMoneyQOQ", todayMoneyQOQ);
        hashMap.put("todayRegister", todayRegister);
        hashMap.put("todayRegisterQOQ", todayRegisterQOQ);
        return hashMap;
    }

    @Override
    public List<Map> getChargingCapacityChargingNumber(String timeType) {
        //获取当前时间
        LocalDateTime todayLocalDateTime = LocalDateTime.now();
        LocalDateTime yesterdayLocalDateTime12 = todayLocalDateTime.minusDays(6);
        String today = todayLocalDateTime.format(DATE_TIME_FORMATTER);
        String sevenDay = yesterdayLocalDateTime12.format(DATE_TIME_FORMATTER);
        List<Map> chargingCapacityChargingNumber = homePageMapper.getChargingCapacityChargingNumber(new BaseEntity(), timeType, "day".equals(timeType) ? today : sevenDay);
        Calendar cal = Calendar.getInstance();
        if ("week".equals(timeType)) {
            chargingCapacityChargingNumber = conversionWeek(chargingCapacityChargingNumber);
        }
        return chargingCapacityChargingNumber;
    }

    @Override
    public List<Map> getChargingStationUsageRate(String timeType) {
        //获取当前时间
        LocalDateTime todayLocalDateTime = LocalDateTime.now();
        LocalDateTime yesterdayLocalDateTime12 = todayLocalDateTime.minusDays(6);
        String today = todayLocalDateTime.format(DATE_TIME_FORMATTER);
        String sevenDay = yesterdayLocalDateTime12.format(DATE_TIME_FORMATTER);
        List<Map> chargingStationUsageRate = homePageMapper.getChargingStationUsageRate(new BaseEntity(), timeType, "day".equals(timeType) ? today : sevenDay);
        int num = homePageMapper.getCountPile();
        if ("week".equals(timeType)) {
            chargingStationUsageRate = conversionWeek(chargingStationUsageRate);
        }
        for (Map map : chargingStationUsageRate) {
            int countNumber = Integer.parseInt(String.valueOf(map.get("countNumber")));
            double mul = ArithmeticUtils.mul(ArithmeticUtils.div(countNumber, num), 100);
            map.put("usageRate", mul);
        }
        return chargingStationUsageRate;
    }

    @Override
    public Map getRegistrationVolume() {
        //获取当前时间
        LocalDateTime todayLocalDateTime = LocalDateTime.now();
        LocalDateTime thisWeekLocalDateTime = todayLocalDateTime.minusDays(6);
        LocalDateTime lastWeekStartLocalDateTime = todayLocalDateTime.minusDays(7);
        LocalDateTime lastWeekLocalDateTime = todayLocalDateTime.minusDays(13);
        String today = todayLocalDateTime.format(DATE_TIME_FORMATTER);
        String thisWeek = thisWeekLocalDateTime.format(DATE_TIME_FORMATTER);
        String lastWeekStart = lastWeekStartLocalDateTime.format(DATE_TIME_FORMATTER);
        String lastWeek = lastWeekLocalDateTime.format(DATE_TIME_FORMATTER);
        List<Map> thisWeekList = homePageMapper.getRegistrationVolume(new BaseEntity(), thisWeek, today);
        List<Map> lastWeekList = homePageMapper.getRegistrationVolume(new BaseEntity(), lastWeek, lastWeekStart);
        Map map = new HashMap();
        map.put("thisWeek", conversionWeek(thisWeekList));
        map.put("lastWeek", conversionWeek(lastWeekList));
        return map;
    }

    @Override
    public List<Map> getTodayStationSumPower() {
        LocalDateTime todayLocalDateTime = LocalDateTime.now();
        String today = todayLocalDateTime.format(DATE_TIME_FORMATTER);
        return homePageMapper.getTodayStationSumPower(new BaseEntity(), today);
    }

    @Override
    public List<ChargeCustomOrder> getTodayChargeOrder() {
        LocalDateTime todayLocalDateTime = LocalDateTime.now();
        String today = todayLocalDateTime.format(DATE_TIME_FORMATTER);
        List<ChargeCustomOrder> todayChargeOrder = homePageMapper.getTodayChargeOrder(new BaseEntity(), today);
        for (ChargeCustomOrder chargeCustomOrder : todayChargeOrder) {
            double mul = ArithmeticUtils.div(chargeCustomOrder.getTotalTime(), 3600);
            chargeCustomOrder.setTotalTime(mul);
        }
        return todayChargeOrder;
    }

    public List<Map> conversionWeek(List<Map> dataList) {
        Calendar cal = Calendar.getInstance();
        for (Map map : dataList) {
            String countTime = String.valueOf(map.get("countTime"));
            try {
                Date date = SIMPLE_DATE_FORMAT.parse(countTime);
                cal.setTime(date);
                int w = cal.get(Calendar.DAY_OF_WEEK) - 1 < 0 ? 0 : cal.get(Calendar.DAY_OF_WEEK) - 1;
                map.put("countTime", weekDays[w]);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        return dataList;
    }


}
